/*
* Copyright© 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.netty.server.server.module.login.service.impl;

import com.laihuichuxing.netty.common.core.exception.ErrorCodeException;
import com.laihuichuxing.netty.common.core.model.Response;
import com.laihuichuxing.netty.common.core.model.ResultCode;
import com.laihuichuxing.netty.common.core.session.Session;
import com.laihuichuxing.netty.common.core.session.SessionManager;
import com.laihuichuxing.netty.server.server.module.login.dao.LoginDao;
import com.laihuichuxing.netty.server.server.module.login.entity.Login;
import com.laihuichuxing.netty.server.server.module.login.service.LoginService;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description: 登录模块
 *
 * @author YangGuang
 * @since: 1.0
 * Create Date Time: 2018/7/16 16:14
 * Update Date Time:
 */
@Component
public class LoginServiceImpl implements LoginService {

    private Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Autowired
    private LoginDao loginDao;

    /**
     * 登录
     *  @param session session
     * @param type    用户类型: 1:乘客 2:车主
     * @param mobile  手机号
     * @param channel
     */
    @Override
    public void login(Session session, int type, String mobile, String token, Channel channel) {
        // 判断是否在其他地方登录过
        boolean isOnline = SessionManager.isOnline(mobile);
        if (isOnline) {
            // 移除会话,关闭连接
            SessionManager.removeSessionCloseSocket(mobile);
        }
        int driverCount=loginDao.seldriver(mobile,token);
        int passengerCount=loginDao.selpassenger(mobile,token);
        if(driverCount>0||passengerCount>0) {
            Login login = new Login();
            login.setType(type);
            login.setMobile(mobile);
            // 判断当前会话是否已登录
            if (session.getAttachment() != null) {
                throw new ErrorCodeException(ResultCode.HAS_LOGIN);
            }
            // 判断用户是否存在
            boolean isExist = true;
            if (type == 1) {
                int count = loginDao.isExistPassenger(mobile);
                if (count == 0) isExist = false;
            } else {
                int count = loginDao.isExistDriver(mobile);
                if (count == 0) isExist = false;
            }
            if (!isExist) {
                throw new ErrorCodeException(ResultCode.USER_NOT_EXIST);
            }
            // 判断是否在其他地方登录过
            isOnline = SessionManager.isOnline(mobile);
            if (isOnline) {
                // 移除会话,关闭连接
                SessionManager.removeSessionCloseSocket(mobile);
            }
            // 加入会话
            if (SessionManager.putSession(mobile, session)) {
                session.setAttachment(login);
                logger.info("手机号为:{}的用户登录了",mobile);
            } else {
                throw new ErrorCodeException(ResultCode.LOGIN_FAIL);
            }
        } else {
            Response response = new Response((short)1, (short)3, null);
            channel.writeAndFlush(response);
        }
    }
}
